home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!vitesse.demon.co.uk
- From: Rich Shepard <Rich@vitesse.demon.co.uk>
- Newsgroups: comp.lang.c++
- Subject: Can't Get A HANDLE (VC++)
- Date: Tue, 26 Mar 96 23:22:41 GMT
- Organization: Myorganisation
- Message-ID: <827882561snz@vitesse.demon.co.uk>
- Reply-To: Rich@vitesse.demon.co.uk
- X-NNTP-Posting-Host: vitesse.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.30
- X-Mail2News-Path: relay-1.mail.demon.net!punt.demon.co.uk!vitesse.demon.co.uk
-
- Hi There,
-
- Sorry about the pun, but I've been trying to redirect the Standard output of a
- child process so that a parent process can read it. It all looks so simple,
- yeah I've done this before (a while ago but.... I understand the basics...
- don't I).
-
- It just doesn't seem to work. The Duplicate handle line always fails. If I miss
- this out the process is spawned but the output is not redirected and the Read
- from pipe at the end just hangs the process!!!! ARGH it looks so simple.
-
- Seems to me that all the examples work fine, it's just when I try to
- incorporate them into my code they don't work. Maybe it's time to quit
- computing and become a bus driver???:)
-
- Thanks In Advance. I would be so grateful if someone would help. You'd
- definately be on my Christmas card list:)
-
- Rich.
-
- //THE NIGHTMARE CODE FOLLOWS!!!!!!!!!!!!!!!!!!!!!!
-
- #define BUFSIZE 2048
-
- void CThreadDlg::OnButton1()
- {
- // TODO: Add your control notification handler code here
- BOOL bSuccess;
- BOOL Flag;
- HANDLE pin;
- HANDLE pout;
- HANDLE DefIn;
- HANDLE DefErr;
- HANDLE DefStd;
- PROCESS_INFORMATION piProcInfo;
- STARTUPINFO siStartInfo;
- SECURITY_ATTRIBUTES saAttr;
- DWORD dwRead;
- CHAR chBuf[BUFSIZE];
-
-
- saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
- saAttr.bInheritHandle = TRUE;
- saAttr.lpSecurityDescriptor = NULL;
-
-
- //Set up members of STARTUPINFO structure.
-
- siStartInfo.cb = sizeof(STARTUPINFO);
- siStartInfo.lpReserved = NULL;
- siStartInfo.lpReserved2 = NULL;
- siStartInfo.cbReserved2 = 0;
- siStartInfo.lpDesktop = NULL;
- siStartInfo.dwFlags = 0;
- // siStartInfo.dwFlags = STARTF_USESTDHANDLES;
-
- // Create the child process.
-
- bSuccess = DuplicateHandle(GetCurrentProcess(),
- GetStdHandle(STD_INPUT_HANDLE),
- GetCurrentProcess(),
- NULL,
- 0,
- FALSE,DUPLICATE_SAME_ACCESS);
-
- if (!CreatePipe(&pin,&pout,&saAttr,1024))
- {
- MessageBox("Pipe Creation Failed");
- }
-
- DefStd = GetStdHandle(STD_OUTPUT_HANDLE);
- DefIn=GetStdHandle(STD_INPUT_HANDLE);
- DefErr=GetStdHandle(STD_ERROR_HANDLE);
- if (!CreatePipe(&pin, &pout, &saAttr, 0))
- {
- MessageBox("Create Pipe Failed");
- }
- if (!SetStdHandle(STD_OUTPUT_HANDLE, pout))
- {
- MessageBox("Redirect Failed");
- }
- if (CreateProcess(NULL,
- "\\MYAPPLICATION", //command line
-
- NULL, // process security attributes
- NULL, // primary thread security attributes
- TRUE, // handles are inherited
- 0, // creation flags
- NULL, // use parent's environment
- NULL, // use parent's current directory
- &siStartInfo, // STARTUPINFO pointer
- &piProcInfo)==FALSE) // receives PROCESS_INFORMATION
- {
- MessageBox("Process Failed");
- }
-
- if (!SetStdHandle(STD_OUTPUT_HANDLE, DefStd))
- {
- MessageBox("StdOut Restore Failed");
- }
-
- Flag=TRUE;
- while (Flag==TRUE)
- {
-
-
- Flag=ReadFile(pin, chBuf, BUFSIZE, &dwRead, NULL);
- if (dwRead == 0)
- {
- Flag=FALSE;
- }
- if (Flag==TRUE)
- {
- MessageBox(chBuf);
- }
- }
-
- }
-
-
- --
- Rich Shepard
-